using System;
using System.Data;
using System.Xml;
using System.Runtime.Serialization;
using System.Collections;
using Widgetsphere.Core.Exceptions;
using Widgetsphere.Core.DataAccess;
using Acme.AuditExample.Business;
using Acme.AuditExample.Business.Objects;
using Acme.AuditExample.Business.Rules;
using Acme.AuditExample.Business.SelectCommands;
using System.Collections.Generic;

namespace Acme.AuditExample.Domain.Objects
{
	/// <summary>
	/// This is an customizable extender for the domain class associated with the 'Customer' object
	/// </summary>
	[Serializable()]
	partial class DomainCustomer : PersistableDomainObjectBase
	{
		#region Member Variables

		private const string TABLE_NAME = "CustomerCollection";
		internal DomainCustomerCollection ParentCol;

		#endregion

		#region Constructor

		internal DomainCustomer(DataRowBuilder rb) : base(rb) 
		{
			this.ParentCol = ((DomainCustomerCollection)(base.Table));
		}

		#endregion

		#region Properties

		internal CustomerPrimaryKey PrimaryKey
		{
			get { return new CustomerPrimaryKey(this.CustomerId); }
		}

		internal System.Guid CustomerId
		{
			get
			{
				try
				{
					return ((System.Guid)(base[this.ParentCol.CustomerIdColumn]));
				}
				catch (InvalidCastException e) { throw new StrongTypingException(GlobalValues.ERROR_PROPERTY_NULL, e); }
			}
		}

		internal string Name
		{
			get
			{
				try
				{
					if (base[this.ParentCol.NameColumn] == System.DBNull.Value) return null;
					else return ((string)(base[this.ParentCol.NameColumn]));
				}
				catch (InvalidCastException e) { throw new StrongTypingException(GlobalValues.ERROR_PROPERTY_NULL, e); }
			}
			set 
			{
				bool wasSet = false;
			  object newValue = DomainHelper.PropertySetterHelper(base[this.ParentCol.NameColumn], value, out wasSet);
				if (wasSet) base[this.ParentCol.NameColumn] = newValue;

				if (wasSet && (this.ParentCol.TableName != TABLE_NAME) && this.ParentCol.SubDomain.Tables.Contains(TABLE_NAME))
				{
					CustomerCollection list = (CustomerCollection)this.ParentCol.SubDomain[Collections.CustomerCollection];
					Customer item = list.GetItemByPK(this.CustomerId);
					if (item != null) item.Name = value;
				}
			}
		}

		#endregion

		#region Collection Operation Methods

		/// <summary>
		/// Serves as a hash function for this particular type.
		/// </summary>
		public override int GetHashCode()
		{
			return base.GetHashCode();
		}

		public override bool Equals(object obj)
		{
			if (obj == null) return false;
			if (obj.GetType() != this.GetType()) return false;

			if ((this.RowState == DataRowState.Deleted) ||
				(((DataRow)obj).RowState == DataRowState.Deleted) ||
				(this.RowState == DataRowState.Detached) ||
				(((DataRow)obj).RowState == DataRowState.Detached))
				return false;

			return ((DomainCustomer)obj).PrimaryKey.Equals(this.PrimaryKey);
		}

		public override void Persist()
		{
			DomainHelper.PersistDomainObject(this, this.ParentCol.SubDomain, Collections.CustomerCollection, this.ParentCol.Modifier);
		}

		public override void Remove()
		{
			this.ParentCol.RemoveCustomer(this);
		}

		#endregion

		#region Relationship Methods
		internal virtual void ReleaseNonIdentifyingRelationships()
		{
		}

		#endregion
	}
}
